/**
  ******************************************************************************
  * @file    sym32l010_hal_vc.h
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   Header file of VC HAL module.
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SYM32L010_HAL_VC_H
#define __SYM32L010_HAL_VC_H

#ifdef __cplusplus
extern "C" {
#endif


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "sym32l010_hal_def.h"


/******************************************************************************/
/* Global type definitions                                                    */
/******************************************************************************/
/* VC负端输入信号 -------------------------------------------------------------*/
typedef enum
{
    VC_NCHANNEL_IN0            = (0x0UL << VCx_CR0_NCHANNEL_Pos),
    VC_NCHANNEL_IN1            = (0x1UL << VCx_CR0_NCHANNEL_Pos),
    VC_NCHANNEL_BGR1V2         = (0x2UL << VCx_CR0_NCHANNEL_Pos),
    VC_NCHANNEL_VCREF          = (0x3UL << VCx_CR0_NCHANNEL_Pos)
} VC_NChannelTypeDef;

/* VC正端输入信号 -------------------------------------------------------------*/
typedef enum
{
    VC_PCHANNEL_IN0            = (0x0UL << VCx_CR0_PCHANNEL_Pos),
    VC_PCHANNEL_IN1            = (0x1UL << VCx_CR0_PCHANNEL_Pos),
    VC_PCHANNEL_IN2            = (0x2UL << VCx_CR0_PCHANNEL_Pos),
    VC_PCHANNEL_IN3            = (0x3UL << VCx_CR0_PCHANNEL_Pos)
} VC_PChannelTypeDef;

/* VC响应速度 -----------------------------------------------------------------*/
typedef enum
{
    VC_RESP_SLOW               = (0x0UL << VCx_CR0_RESP_Pos),         // 响应速度 = 慢
    VC_RESP_FAST               = (0x1UL << VCx_CR0_RESP_Pos),         // 响应速度 = 快
} VC_ResponseTypeDef;

/* VC迟滞 --------------------------------------------------------------------*/
typedef enum
{
    VC_HYS_NONE                = (0x0UL << VCx_CR0_HYS_Pos),          // 没有迟滞
    VC_HYS_25mV                = (0x1UL << VCx_CR0_HYS_Pos),          // 25mV迟滞
} VC_HysteresisTypeDef;

/* 输出信号极性 ---------------------------------------------------------------*/
typedef enum
{
    VC_POLARITY_POSITIVE       = (0x0UL << VCx_CR0_POL_Pos),          // 正端大于负端时输出高电平
    VC_POLARITY_NEGATIVE       = (0x1UL << VCx_CR0_POL_Pos)           // 正端大于负端时输出低电平
} VC_PolarityTypeDef;

/* 窗口比较功能 ---------------------------------------------------------------*/
typedef enum
{
    VC_WINDOW_ENABLE           = (0x1UL << VCx_CR0_WINDOW_Pos),       // 使能窗口比较功能
    VC_WINDOW_DISABLE          = (0x0UL << VCx_CR0_WINDOW_Pos)        // 禁止窗口比较功能
} VC_WindowEnTypeDef;

/* 滤波时钟 -------------------------------------------------------------------*/
typedef enum
{
    VC_FILTERCLK_LSI           = (0x0UL << VCx_CR1_FLTCLK_Pos),       // 滤波时钟为LSI
    VC_FILTERCLK_PCLK          = (0x1UL << VCx_CR1_FLTCLK_Pos)        // 滤波时钟为PCLK
} VC_FilterClkTypeDef;

/* 滤波宽度 -------------------------------------------------------------------*/
typedef enum
{
    VC_FILTERTIME_NONE         = (0x0UL << VCx_CR1_FLTTIME_Pos),      // 关闭滤波
    VC_FILTERTIME_FLTCLK1_N2   = (0x1UL << VCx_CR1_FLTTIME_Pos),      // CLK/1   N=2
    VC_FILTERTIME_FLTCLK1_N4   = (0x2UL << VCx_CR1_FLTTIME_Pos),      // CLK/1   N=4
    VC_FILTERTIME_FLTCLK1_N8   = (0x3UL << VCx_CR1_FLTTIME_Pos),      // CLK/1   N=8
    VC_FILTERTIME_FLTCLK2_N6   = (0x4UL << VCx_CR1_FLTTIME_Pos),      // CLK/2   N=6
    VC_FILTERTIME_FLTCLK2_N8   = (0x5UL << VCx_CR1_FLTTIME_Pos),      // CLK/2   N=8
    VC_FILTERTIME_FLTCLK4_N6   = (0x6UL << VCx_CR1_FLTTIME_Pos),      // CLK/4   N=6
    VC_FILTERTIME_FLTCLK4_N8   = (0x7UL << VCx_CR1_FLTTIME_Pos),      // CLK/4   N=8
    VC_FILTERTIME_FLTCLK8_N6   = (0x8UL << VCx_CR1_FLTTIME_Pos),      // CLK/8   N=6
    VC_FILTERTIME_FLTCLK8_N8   = (0x9UL << VCx_CR1_FLTTIME_Pos),      // CLK/8   N=8
    VC_FILTERTIME_FLTCLK16_N5  = (0xAUL << VCx_CR1_FLTTIME_Pos),      // CLK/16  N=5
    VC_FILTERTIME_FLTCLK16_N6  = (0xBUL << VCx_CR1_FLTTIME_Pos),      // CLK/16  N=6
    VC_FILTERTIME_FLTCLK16_N8  = (0xCUL << VCx_CR1_FLTTIME_Pos),      // CLK/16  N=8
    VC_FILTERTIME_FLTCLK32_N5  = (0xDUL << VCx_CR1_FLTTIME_Pos),      // CLK/32  N=5
    VC_FILTERTIME_FLTCLK32_N6  = (0xEUL << VCx_CR1_FLTTIME_Pos),      // CLK/32  N=6
    VC_FILTERTIME_FLTCLK32_N8  = (0xFUL << VCx_CR1_FLTTIME_Pos)       // CLK/32  N=8
} VC_FilterTimeTypeDef;

/* BLANK持续期间输出电平  ------------------------------------------------------*/
typedef enum
{
    VC_BLANKLEVEL_LOW          = (0x0UL << VCx_CR1_BLKLVL_Pos),
    VC_BLANKLEVEL_HIGH         = (0x1UL << VCx_CR1_BLKLVL_Pos)
} VC_BlankLevelTypeDef;

/* VCREF 输入电压源选择  ----------------------------------------------------------*/
typedef enum
{
    VCREF_VIN_AVCC             = (0x0UL << VCREF_CR_VIN_Pos),
    VCREF_VIN_VCAP             = (0x1UL << VCREF_CR_VIN_Pos)
} VCREF_SourceTypeDef;

/* VC初始化结构体  -------------------------------------------------------------*/
typedef struct
{
    VC_NChannelTypeDef           NChannel;                            // VC负端输入信号
    VC_PChannelTypeDef           PChannel;                            // VC正端输入信号
    VC_WindowEnTypeDef           WindowEn;                            // 窗口比较功能
    VC_PolarityTypeDef           Polarity;                            // 输出信号极性
    VC_HysteresisTypeDef         Hysteresis;                          // VC迟滞
    VC_ResponseTypeDef           Response;                            // VC响应速度
    VC_FilterClkTypeDef          FilterClk;                           // 滤波时钟
    VC_FilterTimeTypeDef         FilterTime;                          // 滤波宽度
} VC_InitTypeDef;


/******************************************************************************/
/* Global pre-processor symbols/macros ('#define')                            */
/******************************************************************************/
/* BLANK触发源，可“或” --------------------------------------------------------*/
#define VC_BLANKSRC_ATIM_OC1REFC        (VCx_CR1_BLKATCH1_Msk)        // ATIM_OC1REFC上升沿触发Blank窗口启动
#define VC_BLANKSRC_ATIM_OC2REFC        (VCx_CR1_BLKATCH2_Msk)        // ATIM_OC2REFC上升沿触发Blank窗口启动
#define VC_BLANKSRC_ATIM_OC3REFC        (VCx_CR1_BLKATCH3_Msk)        // ATIM_OC3REFC上升沿触发Blank窗口启动
#define VC_BLANKSRC_ATIM_OC4REFC        (VCx_CR1_BLKATCH4_Msk)        // ATIM_OC4REFC上升沿触发Blank窗口启动
#define VC_BLANKSRC_ATIM_OC5REFC        (VCx_CR1_BLKATCH5_Msk)        // ATIM_OC5REFC上升沿触发Blank窗口启动
#define VC_BLANKSRC_ATIM_OC6REFC        (VCx_CR1_BLKATCH6_Msk)        // ATIM_OC6REFC上升沿触发Blank窗口启动

/* 中断触发源，可“或” ---------------------------------------------------------*/
#define VC_IT_SOURCE_HIGH               (VCx_CR1_HIGHIE_Msk)          // 高电平触发VC中断
#define VC_IT_SOURCE_RISE               (VCx_CR1_RISEIE_Msk)          // 上升沿触发VC中断
#define VC_IT_SOURCE_FALL               (VCx_CR1_FALLIE_Msk)          // 下降沿触发VC中断

/* VC 状态标志 ----------------------------------------------------------------*/
#define VC_FALG_FLTV                    (VCx_SR_FLTV_Msk)
#define VC_FALG_INTF                    (VCx_SR_INTF_Msk)

/* 配置VC1相应输入管脚为模拟功能 -----------------------------------------------*/
#define AFx_VC1IN0_PB05()               (SYM_GPIOB->ANALOG_f.PIN5 = 1)
#define AFx_VC1IN1_PA00()               (SYM_GPIOA->ANALOG_f.PIN0 = 1)
#define AFx_VC1IN2_PA03()               (SYM_GPIOA->ANALOG_f.PIN3 = 1)
#define AFx_VC1IN3_PB03()               (SYM_GPIOB->ANALOG_f.PIN3 = 1)

/* 配置VC2相应输入管脚为模拟功能 -----------------------------------------------*/
#define AFx_VC2IN0_PB06()               (SYM_GPIOB->ANALOG_f.PIN6 = 1)
#define AFx_VC2IN1_PA05()               (SYM_GPIOA->ANALOG_f.PIN5 = 1)
#define AFx_VC2IN2_PB02()               (SYM_GPIOB->ANALOG_f.PIN2 = 1)
#define AFx_VC2IN3_PB04()               (SYM_GPIOB->ANALOG_f.PIN4 = 1)


/******************************************************************************/
/* Global macro function                                                      */
/******************************************************************************/
/**
  * @brief  获取 VC 中断触发源
  * @param  VCx      : VC 名称
  * @param  ITSOURCE : VC 中断触发源
  *                    @arg VC_IT_SOURCE_HIGH
  *                    @arg VC_IT_SOURCE_RISE
  *                    @arg VC_IT_SOURCE_FALL
  * @retval
  */
#define HAL_VC_GET_IT_SOURCE(VCx, ITSOURCE)   (VCx->CR1 & (ITSOURCE))

/**
  * @brief  使能 VC 中断触发源
  * @param  VCx      : VC 名称
  * @param  ITSOURCE : VC 中断触发源
  *                    @arg VC_IT_SOURCE_HIGH
  *                    @arg VC_IT_SOURCE_RISE
  *                    @arg VC_IT_SOURCE_FALL
  * @retval
  */
#define HAL_VC_ENABLE_IT(VCx, ITSOURCE)       { REGBITS_MODIFY(VCx->CR1, 0x00E0, ITSOURCE); \
                                                VCx->CR0 |= VCx_CR0_IE_Msk; }

/**
  * @brief  关闭 VC 中断触发源
  * @param  VCx      : VC 名称
  * @param  ITSOURCE : VC 中断触发源
  *                    @arg VC_IT_SOURCE_HIGH
  *                    @arg VC_IT_SOURCE_RISE
  *                    @arg VC_IT_SOURCE_FALL
  * @retval
  */
#define HAL_VC_DISABLE_IT(VCx, ITSOURCE)       { VCx->CR1 &= ~((uint32_t)(ITSOURCE)); \
                                                 VCx->CR0 &= ~((uint32_t)VCx_CR0_IE_Msk); }
/**
  * @brief  获取 VC 状态标志
  * @param  VCx  : VC 名称
  * @param  FLAG : VC 状态标志
  *                @arg VC_FALG_READY
  *                @arg VC_FALG_FLTV
  *                @arg VC_FALG_INTF
  * @retval
  */
#define HAL_VC_GET_FLAG(VCx, FLAG)             (VCx->SR & (FLAG))

/**
  * @brief  清除 VC 中断触发标志
  * @param  VCx  : VC 名称
  * @param  FLAG : VC 状态标志
  *                @arg VC_FALG_INTF
  * @retval
  */
#define HAL_VC_CLR_FLAG(VCx, FLAG)             (VCx->SR = ~((uint32_t)(FLAG)))


/******************************************************************************/
/* Exported variables ('extern', definition in C source)                      */
/******************************************************************************/


/******************************************************************************/
/* Global function prototypes ('extern', definition in C source)              */
/******************************************************************************/
void    HAL_VC_Init(VC_TypeDef *VCx, VC_InitTypeDef *InitStruct);
void    HAL_VC_DeInit(VC_TypeDef *VCx);

void    HAL_VCReference_Enable(VCREF_SourceTypeDef VCrefVin, uint8_t DivValue);
void    HAL_VCReference_Disable(void);

void    HAL_VC_Enable(VC_TypeDef *VCx);
void    HAL_VC_Disable(VC_TypeDef *VCx);

uint8_t HAL_VC_GetOutputValue(VC_TypeDef *VCx);

void    HAL_VC_Blank_Config(VC_TypeDef *VCx,
                            uint32_t TriggerSource,
                            uint16_t BlankTime,
                            VC_BlankLevelTypeDef BlankLevel);


#ifdef __cplusplus
}
#endif

#endif /* __SYM32L010_HAL_VC_H */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
